package com.qf.action;

import com.qf.exception.YunException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class BaseAction {
    //处理整个系统的异常，响应json
    //注解的异常处理器
    @ResponseBody
    @ExceptionHandler
    public Map<String,Object> allError(HttpServletRequest request,Exception exp){
        Map<String,Object> map=new ConcurrentHashMap<>();
        YunException yunException=null;
        if (exp instanceof YunException){
            yunException=(YunException)exp;
        }else{
            yunException=new YunException("系统忙着呢",-1);
        }
        return yunException.toMap();
        //方法2：
        /*map.put("code",yunException.getCode());
        map.put("errormsg",yunException.getMessage());
        return map;*/
    }
}
